import bmesh
import bpy
from bpy.types import Operator

class Draw_Face_Op(Operator):
    bl_idname = "object.draw_face"
    bl_label = "draw face"
    bl_description = "draw a face"

    def execute(self,context):
        verts = ((0, 3),(2.5, 0.5),(5, 1),(4.5, 3.5),(10.5, 2),(8, 10),(7, 4.5),(2, 6))
        bm = bmesh.new()
        count = 0
        for v in verts:
            count = count +1
            bm.verts.new((v[0], v[1], count))
        bm.faces.new(bm.verts)

        bm.normal_update()

        me = bpy.data.meshes.new("")
        bm.to_mesh(me)

        ob = bpy.data.objects.new("", me)
        bpy.context.scene.collection.objects.link(ob)

        # 画射线
        result = ob.ray_cast([2,2,0], [2,2,100])

        verts = [(result[1][0],result[1][1],result[1][2]),(result[1][0],result[1][1],0),(result[1][0],result[1][1],10),(3,3,0),(3,3,10)]
        lines = [(0,1),(0,2),(3,4)]

        # Create Mesh Datablock
        mesh = bpy.data.meshes.new('point')
        mesh.from_pydata(verts, lines, [])

        obj = bpy.data.objects.new('ray', mesh)
        bpy.context.scene.collection.objects.link(obj)
        
        return {'FINISHED'}